import 'package:enterprise_v6_app/app/components/custom_modele/custom_card.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_search_enter.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/basic_information/basic_department_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_response_card_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/emergency/resource/card/card_controller.dart';

/// 应急处置卡
class EmergencyResourceCardPage extends StatelessWidget {
  EmergencyResourceCardPage({super.key});

  final controller = Get.put(EmergencyResourceCardController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: controller,
      builder: (_) {
        return Scaffold(
          body: Container(
            alignment: Alignment.topCenter,
            color: const Color(0xffF5F5F6),
            child: Column(
              children: [
                _buildTitle(),
                AppGaps.vGap10,
                _buildMain(),
              ],
            ),
          ),
        );
      },
    );
  }

  Widget _buildMain() {
    return CustomCard(
      padding: EdgeInsets.symmetric(horizontal: 15.w, vertical: 13.w),
      content: controller.selectedDepartment == null
          ? Column(
              children: controller.departments.map(_buildInitDepartmentItem).toList(),
            )
          : Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    SizedBox(
                      height: 37.w,
                      width: 203.w,
                      child: CustomSearchEnter(
                        hintText: '搜索应急处置卡、人员',
                        onCallBack: (value) {
                          controller.search = value;
                          controller.refreshPage();
                        },
                      ),
                    ),
                    Text(
                      '共${controller.totalCount}张',
                      style: TextStyle(
                        fontSize: 12.w,
                        color: const Color(0xFFA0A7B5),
                      ),
                    ),
                  ],
                ),
                AppGaps.vGap20,
                SizedBox(
                  height: 517.w,
                  child: _buildCards(),
                ),
              ],
            ),
    );
  }

  /// 最开始选择的部门
  Widget _buildInitDepartmentItem(BasicDepartmentEntity entity) {
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(7.w),
        color: const Color(0x1A5C6DFF),
      ),
      margin: EdgeInsets.symmetric(vertical: 7.w),
      padding: EdgeInsets.symmetric(horizontal: 7.w, vertical: 7.w),
      child: Row(
        children: [
          Container(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(13.w),
              color: Colors.white,
            ),
            padding: EdgeInsets.all(3.w),
            child: Image.asset(
              AppImages.icon_person_double_blue,
              width: 13.w,
              height: 13.w,
            ),
          ),
          AppGaps.hGap10,
          SizedBox(
            width: 183.w,
            child: Text(
              entity.departmentName,
              style: TextStyle(
                fontSize: 14.w,
                color: const Color(0xff333333),
              ),
              overflow: TextOverflow.ellipsis,
            ),
          ),
          AppGaps.hGap10,
          Container(
            width: 21.w,
            height: 21.w,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(3.w),
              color: const Color.fromARGB(255, 15, 147, 255)
            ),
            child: Icon(Icons.arrow_forward, size: 12.w, color: Colors.white),
          ),
        ],
      ),
    ).clickOnce(() {
      controller.selectedDepartment = entity;
      controller.update();
    });
  }

  /// 顶部的部门指示条
  Widget _buildTitle() {
    return CustomCard(
      content: Row(
        children: [
          Text(
            '持卡部门',
            style: TextStyle(
              fontWeight: FontWeight.w500,
              color: controller.selectedDepartment != null ? Colors.blue : const Color(0xFF495061),
              fontSize: 12.w,
            ),
          ).clickOnce(() {
            controller.selectedDepartment = null;
            controller.update();
          }),
          AppGaps.hGap4,
          if (controller.selectedDepartment != null)
            Text(
              '/',
              style: TextStyle(fontSize: 12.w, color: Colors.grey),
            ),
          AppGaps.hGap4,
          if (controller.selectedDepartment != null)
            Text(
              controller.selectedDepartment!.departmentName,
              style: TextStyle(
                fontWeight: FontWeight.w500,
                color: const Color(0xFF495061),
                fontSize: 12.w,
              ),
            ),
        ],
      ),
    );
  }

  /// 应急处置卡
  Widget _buildCards() {
    return CustomEasyRefreshList<EmergencyResponseCardEntity>(
      key: controller.globalKey,
      dataLoad: (pageIndex, pageSize, {String? search}) async {
        return controller.getListData(pageIndex, pageSize);
      },
      itemBuilder: (item, index) {
        final cardHolderCount = () {
          if (item.cardholder.isEmpty) return 0;
          return item.cardholder.split(',').length;
        }();
        final url = () {
          if (item.frontView.isEmpty && item.backView.isEmpty) return null;
          if (item.frontView.isNotEmpty) return item.frontView;
          return item.backView;
        }();
        return Container(
          padding: EdgeInsets.all(13.w),
          decoration: BoxDecoration(color: const Color(0x335C6DFF), borderRadius: BorderRadius.circular(7.w)),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                item.nameOfEmergencyResponseCard,
                style: TextStyle(
                  color: const Color(0xFF252F81),
                  fontSize: 16.w,
                  fontWeight: FontWeight.w500,
                ),
              ),
              AppGaps.vGap12,
              Container(
                height: 91.w,
                width: 231.w,
                decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(7.w)),
                child: url != null ? Image.network(Interface.fileUrl + url) : const SizedBox(),
              ),
              AppGaps.vGap12,
              Text(
                '持卡人（共$cardHolderCount人）',
                style: TextStyle(
                  fontSize: 13.w,
                  color: const Color(0xFF5C6DFF),
                ),
              ),
            ],
          ),
        ).clickOnce(() {
          // Get.toNamed(
          //   AppRoutes.emergencyResourceDetails,
          //   arguments: {
          //     'type': EmergencyResourceDetailPageType.card,
          //     'data': item,
          //   },
          // );
        });
      },
    );
  }
}
